<?php

namespace Plu\qdh\Services;

use Plu\qdh\Services\ServiceBase;
use OSS\OssClient;
use OSS\Core\OssException;

class FileService extends ServiceBase {

    // 阿里云OSS图片上传
    public function uploadFile($filePath, $fileName) {

        $accessKeyId     = \AliCloud::OSS_ACCESSKEYID;

        $accessKeySecret = \AliCloud::OSS_ACCESSKEYSECRET;

        $endpoint = \AliCloud::ENDPOINT;

        $bucket   = \AliCloud::BUCKET_NAME;
        
        try{
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ossClient->uploadFile($bucket, $fileName, $filePath);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            exit;
        }
        $this->removeTemFile($filePath);
        return \AliCloud::BUCKET_URL.$fileName;

    }

    private function removeTemFile($filePath) {
        unlink($filePath);
    }





}